package client.ftp;

import com.zehon.exception.FileTransferException;
import com.zehon.sftp.SFTPClient;
import java.io.File;

/**
 * Responsible for file transfer using SFTP. Uses the Zehon library instead of
 * ftp4j.
 * @author Robert
 */
public class SFTPHandler extends FileTransfer {
    private String privateKeyPath;
    private Boolean usePrivateKey;
    SFTPClient sftp;

    /**
     * Sole constructor.
     * @param hostname the name of the server to connect to (IP address or domain name)
     * @param username the user name of the account
     * @param password the password of the account
     * @param port the port number to connect to
     * @param privateKeypath the path of the private key file
     * @param usePrivateKey whether or not to use a private key
     */
    public SFTPHandler(String hostname, String username, String password, int port, String privateKeyPath, Boolean usePrivateKey) {
        super(hostname, username, password, port);
        this.privateKeyPath = privateKeyPath;
        this.usePrivateKey = usePrivateKey;
    }

    /**
     * Uploads a file to an SFTP server
     * @param localFile The name of the file to be uploaded
     * @param remoteFolder The directory on the server where file should be put
     * @return An Integer indicating success(1) or failure
     */
    protected int upload(String localFile, String remoteFolder) {
        /**
         * detect whether or not to use private key, then construct new object
         */
        if (usePrivateKey == true) {
            sftp = new SFTPClient(hostname, port, username, privateKeyPath, usePrivateKey);
        } else {
            sftp = new SFTPClient(hostname, port, username, password);
        }

        /**
         * send file and output corresponding message
         */
        try {
            int status = sftp.sendFile(localFile, remoteFolder);
            if (status == 1) {
                printMessage("Connected to " + hostname + " at port "+port+" using SFTP\n" + "Logged in with " + username + "\n" + "Successfully uploaded file " + localFile + " to " + hostname + remoteFolder);
                return 0;
            } else if (status == 0) {
                printMessage("Unknown Error. Check connection.");
                return -1;
            } else if (status == 2) {
                printMessage("Error: Invalid settings, check username,password, hostname, keypath...");
                return 4;
            } else if (status == 3) {
                printMessage("Error: File does not exist.");
                return 5;
            }
            return status;
        } catch (FileTransferException ex) {
            printError("Error: ", ex);
            return -1;
        }
    }

    /**
     * Downloads a file from an SFTP server
     * @param remoteFile The name of the file to be downloaded
     * @param remoteFolder The directory on the server where the file can be found
     * @param localFolder The local directory where the file should be put
     * @return An Integer indicating success(0) or failure
     */
    protected int download(String remoteFile, String remoteFolder, String localFolder) {
        /**
         * detect whether or not to use private key, then construct new object
         */
        if (usePrivateKey == true) {
            sftp = new SFTPClient(hostname, port, username, privateKeyPath, usePrivateKey);
        } else {
            sftp = new SFTPClient(hostname, port, username, password);
        }
        
        /**
         * retrieve file and output corresponding message
         */
        try {
            String fileName = new File(remoteFile).getName();
            int status = sftp.getFile(fileName, remoteFolder, localFolder);
            if (status == 1) {
                printMessage("Connected to " + hostname + " at port "+port+" using SFTP\n" + "Logged in with " + username + "\n" + "Successfully downloaded file " + remoteFolder + remoteFile + " to " + localFolder);
                return 0;
            } else if (status == 0) {
                printMessage("Unknown Error. Check connection.");
                return -1;
            } else if (status == 2) {
                printMessage("Error: Invalid settings, check username,password, hostname.");
                return 4;
            } else if (status == 3) {
                printMessage("Error: File does not exist.");
                return 5;
            }
            return status;
        } catch (FileTransferException ex) {
            printError("Error: ", ex);
            return -1;
        }
    }
}